<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="分类名" prop="name">
        <el-input maxlength="100" clearable v-model="queryParams.name" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="menu"><el-button @click="handleAdd" type="primary" icon="el-icon-circle-plus-outline"
        size="mini">新增</el-button></div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="编号" width="180">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="180">
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间">
      </el-table-column>
      <el-table-column prop="createBy" label="创建人">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <a href="#" @click="handleAddArticle(scope.row)" style="color:#1890FF;">新增文章</a>
          <a href="#" @click="handleUpdate(scope.row)" style="color:#1890FF; margin-left: 20px;">编辑</a>
          <a href="#" @click="handleDelete(scope.row)" style="color:red; margin-left: 20px;">删除</a>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入标签名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { class_queryPage, class_delete, class_insert, class_update } from "@/api/fenlei"

export default {
  name: 'Index',
  components: {

  },
  data() {
    return {
      queryParams: {
        name: '',
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "标签名称不能为空", trigger: "blur" }
        ],
      }
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      class_queryPage(this.queryParams).then(res => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标签";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      this.form = row;
      this.open = true;
      this.title = "修改标签";
      // const id = row.id || this.ids
      // getMeltingCrane(id).then(response => {
      // this.form = response.data;
      // this.open = true;
      // this.title = "修改标签";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            class_update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            class_insert(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return class_delete(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    //新增文章
    handleAddArticle(row) {
      this.$router.push({
        path: "/knowledge/add",
        query: { classid: row.id },
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.menu {
  display: flex;
  justify-content: end;
  margin-bottom: 15px;
}

.item {
  cursor: pointer;
  font-size: 16px;
  padding: 16px 24px 12px 24px;
  border-bottom: 1px solid #f0f2f5;

  .title {
    font-weight: bold;
  }

  .jianjie {
    color: #555666;
    margin-top: 8px;
  }

  .desc {
    margin-top: 8px;
    display: flex;
    align-items: center;

    .biaoqian {
      color: #fc5531;
      background-color: #fff5f2;
      padding: 3px 8px;
    }

    .zuozhe {
      margin-left: 15px;
    }
  }
}
</style>
